/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function validateContent(){
    var content = document.getElementById("areaComment").value;
    if(content.length < 1 ||  content.length > 500){
        document.getElementById("commentStatus").style.backgroundColor = "#FFFF66";
        document.getElementById("commentStatus").innerHTML = "Bạn chưa nhập bình luận.";
        document.getElementById("areaComment").focus();
        return false;
    }
    return true;
}

function validateFullNameCM(){
    var fullName = document.getElementById("txtName").value;
    if(fullName.length < 1 ||  fullName.length > 50){
        document.getElementById("commentStatus").style.backgroundColor = "#FFFF66";
        document.getElementById("commentStatus").innerHTML = "Tên không hợp lệ.";
        document.getElementById("txtName").focus();
        return false;
    }
    return true;
}

function validateEmailCM(){
    var email = document.getElementById("txtEmail").value;
    var fil = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if(!fil.test(email)){
        document.getElementById("commentStatus").style.backgroundColor = "#FFFF66";
        document.getElementById("commentStatus").innerHTML  = "Email không hợp lệ.";
        document.getElementById("txtEmail").focus();
        return false
    }else if(email.length < 1 || email.length > 50){
        document.getElementById("commentStatus").style.backgroundColor = "#FFFF66";
        document.getElementById("commentStatus").innerHTML  = "Email không hợp lệ.";
        document.getElementById("txtEmail").focus();
        return false;
    }
    return true;
}

function validateComment(){
    
    var checkCM = true;
    if(!validateEmailCM()){
        checkCM = false;
    }
    
    if(!validateFullNameCM()){
        checkCM = false;
    }
    
    if(!validateContent()){
        checkCM = false;
    }

    if(checkCM==true){
        insertComment();
    }

}

// Function to insert comment
function insertComment()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            // Display status
            document.getElementById("commentStatus").style.backgroundColor = "#FFFF66";
            document.getElementById("commentStatus").innerHTML=xmlhttp.responseText;
        }
    }
    // Get parameter
    var name = document.getElementById("txtName").value;
    var email = document.getElementById("txtEmail").value;
    var content = document.getElementById("areaComment").value;
    var infoId = document.getElementById("txtInfoId").value;
    
    // Get date and time
    var d = new Date();
    var date = d.getDate() + "-" + (d.getMonth()+1) + "-" + d.getFullYear() + " " + d.getHours() + "-" + d.getMinutes();

    xmlhttp.open("POST","Controller",true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var url = "action=insertComment&name="+name+"&email="+email+"&content="+content+"&date="+date+"&infoId="+infoId;
    xmlhttp.send(url);


    // Add new div after div parent
    var div = document.createElement("div");
    div.setAttribute("class", "comment-info");
    div.innerHTML = "<div class='label2'><span>"+name+" - " + email + "</span><span style='float:right;'>"+date+"</span></div><div class='label1'><p>"+content+"</p></div>";
    var main = document.getElementById("mainCm");
    main.appendChild(div);
    // Delete text after comment
    document.getElementById("txtName").value = "";
    document.getElementById("txtEmail").value= "";
    document.getElementById("areaComment").value= "";

    window.setTimeout(function(){
        document.getElementById("commentStatus").style.opacity = "0"
    }, 5000);
}
